{% extends 'base/base.html' %}
{% load i18n %}

{% block title %}{{ title }} - {{ block.super }}{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{{ title }}</h5>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        
                        <div class="mb-3">
                            <label for="{{ form.title.id_for_label }}" class="form-label">
                                {{ form.title.label }}
                            </label>
                            {{ form.title }}
                            {% if form.title.errors %}
                                <div class="text-danger small">
                                    {{ form.title.errors }}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="mb-3">
                            <label for="{{ form.content.id_for_label }}" class="form-label">
                                {{ form.content.label }}
                            </label>
                            {{ form.content }}
                            {% if form.content.errors %}
                                <div class="text-danger small">
                                    {{ form.content.errors }}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">
                                {{ form.tags.label }}
                                <span class="text-muted">({% trans '可选' %})</span>
                            </label>
                            <div class="border rounded p-3">
                                {% if form.tags.field.queryset.exists %}
                                    {% for tag in form.tags.field.queryset %}
                                        <div class="form-check">
                                            <input type="checkbox" 
                                                   class="form-check-input" 
                                                   name="tags" 
                                                   value="{{ tag.id }}" 
                                                   id="tag_{{ tag.id }}"
                                                   {% if tag in form.instance.tags.all or form.tags.value and tag.id|stringformat:"s" in form.tags.value %}checked{% endif %}>
                                            <label class="form-check-label" for="tag_{{ tag.id }}">
                                                {{ tag.name }}
                                                <span class="badge ms-2" style="background-color: {{ tag.color }};">
                                                    &nbsp;
                                                </span>
                                            </label>
                                        </div>
                                    {% endfor %}
                                {% else %}
                                    <p class="text-muted mb-0">{% trans '暂无可用标签' %}</p>
                                {% endif %}
                            </div>
                            {% if form.tags.errors %}
                                <div class="text-danger small">
                                    {{ form.tags.errors }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                {{ form.tags.help_text|default:"选择要发布公告的目标项目标签，不选择则为全局公告" }}
                            </small>
                        </div>
                        
                        <div class="mb-3">
                            <div class="form-check">
                                {{ form.is_active }}
                                <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                                    {{ form.is_active.label }}
                                </label>
                            </div>
                            {% if form.is_active.errors %}
                                <div class="text-danger small">
                                    {{ form.is_active.errors }}
                                </div>
                            {% endif %}
                            <small class="form-text text-muted">
                                {{ form.is_active.help_text }}
                            </small>
                        </div>
                        
                        <div class="d-flex justify-content-between">
                            <a href="{% url 'projects:announcement_list' %}" class="btn btn-secondary">
                                <i class="fa fa-arrow-left"></i> {% trans '返回列表' %}
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="fa fa-save"></i> {% trans '保存公告' %}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}